package com.fz.song.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 执行时间AOP
 */

@Aspect
public class ExecuteTimeAOP {

    ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    private static Log logger = LogFactory.getLog(ExecuteTimeAOP.class);

    @Pointcut(value = "execution(* com.fz.song.service.*.*.*(..))")
    public void point() {

    }


    @Before(value = "point()")
    public void before(JoinPoint jp) {
        threadLocal.set(System.currentTimeMillis());
    }

    @AfterReturning(value = "point()")
    public void after(JoinPoint joinPoint) {
        Long start = threadLocal.get();
        long end = System.currentTimeMillis();
        long time = end - start;
        String name = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        logger.info(name + "的" + methodName + "方法的执行时间:" + time + "毫秒");
    }


}
